package com.jiuyin.dianjing.ui.fragment.search;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.DefaultItemAnimator;

import com.bumptech.glide.Glide;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.BaseRecyclerAdapter;
import com.jiuyin.dianjing.adapter.SmartViewHolder;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.enums.SearchFragment;
import com.jiuyin.dianjing.event.GameInfoResultMsgEvent;
import com.jiuyin.dianjing.event.MsgDispatcher;
import com.jiuyin.dianjing.event.WordMsgEvent;
import com.jiuyin.dianjing.model.GameInfoItemModel;
import com.jiuyin.dianjing.ui.activity.gameInfo.GameInfoDetailActivity;
import com.jiuyin.dianjing.ui.fragment.base.BaseRefreshFragment;
import com.jiuyin.dianjing.util.LogUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.HashMap;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_HEAD_URL;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_INFORMATIONID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_NUM;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TITLE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_URL;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_WORD;
import static com.jiuyin.dianjing.api.constant.ApiConstant.PAGE_FIRST;
import static com.jiuyin.dianjing.api.constant.ApiConstant.PAGE_NUM;

/*
 *
 * 游戏资讯搜索结果列表界面
 */
public class SearchGameInfoResultFragment extends BaseRefreshFragment<GameInfoItemModel> {
    private GameInfoResultMsgEvent mGameInfoResultMsgEvent;

    public SearchGameInfoResultFragment() {
        super(PAGE_NUM);
        EventBus.getDefault().register(this);
    }

    /**
     * 通过关键字像服务器请求搜索内容
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void searchRequest(GameInfoResultMsgEvent event) {
        mPageNum = PAGE_FIRST;
        mGameInfoResultMsgEvent = event;
        doSearchRequest();
    }

    @Override
    public void fetchData() {
        doSearchRequest();
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_search_result;
    }

    @Override
    protected void initData() {
        mAdapter = new BaseRecyclerAdapter<GameInfoItemModel>(R.layout.item_info_game) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, GameInfoItemModel model, int position) {
                if (null != model) {
                    LinearLayout typeAd = holder.itemView.findViewById(R.id.ll_type_ad);
                    typeAd.setVisibility(View.GONE);
                    holder.text(R.id.tv_game_info_title, model.getTitle());
                    holder.text(R.id.tv_game_info_comment, model.getComment());
                    holder.text(R.id.tv_game_info_check, model.getCheck());
                    holder.text(R.id.tv_game_info_time, model.getTime());
                    ImageView imageView = holder.itemView.findViewById(R.id.img_game);
                    Glide.with(getActivity()).load(model.getShareUrl()).into(imageView);
                }
            }
        };
        mAdapter.setOnItemClickListener((parent, view, position, id) -> {
            GameInfoItemModel model = mAdapter.getItem(position);
            Bundle bundle = new Bundle();
            bundle.putString(KEY_URL, model.getInfoUrl());
            bundle.putString(KEY_HEAD_URL, model.getShareUrl());
            bundle.putString(KEY_INFORMATIONID, model.getInformationId());
            bundle.putString(KEY_TITLE, model.getTitle());
            goTargetActivity(GameInfoDetailActivity.class, bundle);
        });
        mRcvPage.setItemAnimator(new DefaultItemAnimator());
        mRcvPage.setAdapter(mAdapter);
    }

    @Override
    protected void initView() {
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 向服务器请求关键搜索内容
     */
    private void doSearchRequest() {
        if (null != mGameInfoResultMsgEvent) {
            String msg = mGameInfoResultMsgEvent.getMsg();
            String type = mGameInfoResultMsgEvent.getType();
            LogUtil.log("SearchGameInfoResultFragment doSearchRequest msg = " + msg + ",type = " + type);
            MsgDispatcher msgDispatcher = new MsgDispatcher(SearchFragment.SUGGEST);
            msgDispatcher.setWordMsgEvent(new WordMsgEvent(msg));
            EventBus.getDefault().post(msgDispatcher);
            HashMap<String, Object> params = new HashMap<>();
            params.put(KEY_WORD, msg);
            params.put(KEY_NUM, mPageNum);
            params.put(KEY_TOKEN, HelperApplication.getToken());
            params.put(KEY_TYPE, type);
            JSONObject jsonObject = new JSONObject(params);
            getApiHelper().appGetHomeSearchByWord(jsonObject.toString(), data -> {
                dealWithSuccessInfo(data);
            });
        }
    }
}